<template>
	<view class="film">
		<u-sticky>
			<view class="sticky">
				<u-row>
					<u-col :span="4">
						<u-button @click="openSiteSelect" size="mini">网站: {{ site.name }}</u-button>
					</u-col>
					<u-col :span="8">
						<view class="header">
							<u-search v-model="search" @search="searchEvent" @custom="searchEvent" clearabled
								@clear="searchClearEvent"></u-search>
						</view>
					</u-col>
				</u-row>
				<u-tabs :list="typeList" :current="currentClass" @change="changeClass"></u-tabs>
			</view>
		</u-sticky>
		<view class="btns">
			<text>共: {{ listQuery.totalCount }} 资源</text>
		</view>
		<mescroll-body ref="mescrollRef" @down="downCallback" :bottombar="false" :down="downOption" :sticky="false"
			@up="upCallback" :up="upOption" @init="mescrollInit">
			<view class="body">
				<u-waterfall v-model="flowList" ref="uWaterfall">
					<template v-slot:left="{ leftList }">
						<view class="box-warter left-box-warter" v-for="(item, index) in leftList" :key="index"
							@click="openDetail(item)">
							<u-lazy-load border-radius="4" :image="item.vod_pic" :index="index"></u-lazy-load>
							<view class="box-name">{{ item.vod_name }}</view>
							<view class="box-info">
								<view class="box-class">{{ item.type_name }}</view>
								<view class="box-class">{{ item.vod_sub }}</view>
								<view class="box-year">{{ item.vod_year }}</view>
							</view>
						</view>
					</template>
					<template v-slot:right="{ rightList }">
						<view class="box-warter right-box-warter" v-for="(item, index) in rightList" :key="index"
							@click="openDetail(item)">
							<u-lazy-load threshold="-450" border-radius="4" :image="item.pic|| item.vod_pic"
								:index="index"></u-lazy-load>
							<view class="box-name">{{ item.vod_name }}</view>
							<view class="box-info">
								<view class="box-class">{{ item.type_name }}</view>
								<view class="box-class">{{ item.vod_sub }}</view>
								<view class="box-year">{{ item.vod_year }}</view>
							</view>
						</view>
					</template>
				</u-waterfall>
			</view>
		</mescroll-body>
		<u-top-tips ref="uTips"></u-top-tips>
		<u-toast ref="uToast" />
		<SelectSite ref="refSelectSite" @change="siteConfirm"></SelectSite>
	</view>
</template>

<script>
	import http from "@/utils/request.js"
	import MescrollMixin from "@/libs/mescroll-uni/components/mescroll-uni/mescroll-mixins.js"
	import SelectSite from "./components/selectSite.vue"
	export default {
		mixins: [MescrollMixin],
		components: { SelectSite },
		data() {
			return {
				search: "",
				site: {
					name: '加载中'
				},
				siteList: [],
				type: {},
				currentClass: 0,
				typeList: [],
				flowList: [{
					"vod_time": "2023-10-07 13:45:02",
					"vod_id": "5632",
					"vod_name": "乌当堂堂家族",
					"vod_enname": "wudangtangtangjiazu",
					"vod_subname": "",
					"vod_letter": "W",
					"vod_color": "",
					"vod_tag": "",
					"type_id": "20",
					"type_name": "欧美剧",
					"vod_pic": "https://pic1.bdzyimg.com/upload/vod/20231003-1/c5ce83fdef25b288e6e0055a4fac2cf9.jpg"
				}],
				r18KeyWords: [],
				config: {
					R18: false,
				},
				listQuery: {
					page: 1,
					pageSize: 20,
					pageCount: 0,
					totalCount: 0,
				},
				mescroll: null,
				downOption: {
					use: true,
					auto: true
				},
				upOption: {
					page: {
						num: 0,
						size: 20,
						time: null
					},
					empty: {
						use: true,
						icon: null,
						tip: "暂无数据",
						fixed: false,
						top: "560rpx",
					},
					textNoMore: '没有更多数据',
					toTop: {
						warpId: null,
						src: null,
						html: null,
						offset: 1000,
						warpClass: "mescroll-totop",
						showClass: "mescroll-fade-in",
						hideClass: "mescroll-fade-out",
						duration: 300,
						supportTap: false,
						btnClick: null
					}
				}
			}
		},
		onLoad() {
			this.flowList = new Array()
			this.getSite()
		},
		onTabItemTap() {
			
		},
		methods: {
			upCallback(options) {
				this.listQuery.page = options.num
				this.listQuery.pageSize = options.size
				if (this.listQuery.pageCount > 0) {
					this.fetchData()
				} else {
					this.mescroll.endSuccess(0, false)
				}
			},
			// 下拉刷新的回调 (mixin默认resetUpScroll)
			downCallback() {
				this.listQuery.page = 1
				if (this.mescroll.optUp.use) {
					this.mescroll.resetUpScroll()
				} else {
					setTimeout(() => {
						this.mescroll.endSuccess();
					}, 500)
				}
			},
			async mescrollInit(mescroll) {
				this.mescroll = mescroll
				await this.getSite()
			},
			async fetchData() {
				if (this.listQuery.page == 1) {
					this.flowList = []
					this.$refs.uWaterfall.clear();
				}
				let rowCount = await this.addData(this.site, this.type.tid)
				let next = this.listQuery.page < this.listQuery.pageCount
				// console.log(rowCount)
				// console.log(next)
				this.mescroll.endSuccess(rowCount, next)
			},
			fetchR18Keyword() {
				this.r18KeyWords = this.settings.r18Keywords
				this.config.R18 = this.settings.R18
			},
			async searchEvent() {
				try{
					this.flowList = []
					this.$refs.uWaterfall.clear();
					const list = await http.search(this.site, this.search)
					// console.log(res)
					if (list.length == 0) {
						return
					}
					for (const i of list) {
						const data = await http.detail(this.site, i.vod_id)
						// console.log(data)
						this.flowList.push(data.data)
					}
				}catch(e){
					this.$u.toast('搜索失败' + e)
				}
	
			},
			async searchClearEvent() {
				this.search = ''
				this.flowList = []
				this.$refs.uWaterfall.clear();
				this.listQuery.page = 1
				this.mescroll.resetUpScroll()
			},
			openSiteSelect() {
				this.$refs.refSelectSite.handleShow(this.site)
			},
			async siteConfirm(site) {
				if (this.site.key == site.key) {
					return
				}
				this.$u.vuex('defcku.siteKey', site.key)
				this.listQuery.page = 1
				this.flowList = []
				this.$refs.uWaterfall.clear();
				this.site = site
				// console.log(site) 
				await this.getClass(this.site)
				await this.getPage()
			},
			openDetail(item) {
				const url = `/pages/play/play`
				this.$u.route({
					url: url,
					params: {
						site: this.site.key,
						id: item.vod_id
					}
				})
			},
			async getSite() {
				let sites = this.defcku.sites
				if (sites.length == 0) {
					this.site.name = '未配置'
					this.$u.toast('还未配置视频源')
					console.log('还未配置视频源')
					this.mescroll.endSuccess()
					return
				}
				let site = sites.find(t => t.key == this.defcku.siteKey)
				if (!site) {
					site = sites[0]
					this.$u.vuex('defcku.siteKey', site.key)
				}
				this.site = site
				await this.fetchR18Keyword()
				await this.getClass(site)
				await this.getPage()
			},
			async getPage(type) {
				const res = await http.page(this.site, type)
				this.listQuery.pageCount = res.pagecount
				this.listQuery.totalCount = res.recordcount
				if (this.listQuery.totalCount == 0) {
					this.flowList = []
					this.$refs.uWaterfall.clear();
				} else {
					this.mescroll.resetUpScroll()
				}
			},
			async getClass(site) {
				this.typeList = [{ name: '最新', tid: 0 }]
				this.type = { name: '最新', tid: 0 }
				let classToHide = []
				try {
					if (this.config.R18) {
						this.r18KeyWords.map(t => classToHide.push(t))
					}
					this.currentClass = 0
					let data = await http.class(site)
					data.forEach(t => {
						if (!classToHide.includes(t.name)) {
							this.typeList.push(t)
						}
					})
				} catch (e) {
					console.log(e)
					this.$u.toast('分类数据加载失败')
				}
			},
			async addData(site, t) {
				let page = this.listQuery.page++
				console.log(`正在加载第${page}页数据`)

				const res = await http.list(site, page, t)
				for (let i = 0; i < res.length; i++) {
					let item = res[i]
					if (this.config.R18) {
						const nameLike = this.r18KeyWords.some(v => item.vod_name.includes(v))
						// console.log("nameLike=" + nameLike) 
						if (nameLike) {
							this.listQuery.totalCount--
							continue
						}
						const typeLike = this.r18KeyWords.some(v => item.type_name && item.type_name.includes(v))
						if (typeLike) {
							this.listQuery.totalCount--
							continue
						}
						this.flowList.push(item);
					} else {
						this.flowList.push(item);
					}
				}
				return res.length
			},
			changeClass(index) {
				this.currentClass = index
				this.type = this.typeList[index]
				this.listQuery.page = 1
				this.getPage(this.type.tid)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.film {
		background-color: #f8f8f8;

		.sticky {
			background-color: #fff;
			min-height: 60px;
		}

		.btns {
			margin-top: 10rpx;
			display: flex;
			justify-content: space-around;
			align-items: center;
		}

		.body {
			.box-warter {
				border-radius: 8rpx;
				padding: 14rpx;
				background-color: #fff;

				.box-info {
					margin-top: 10rpx;
					display: flex;
					justify-content: space-between;
					font-size: 12px;
				}
			}

			.left-box-warter {
				margin: 10rpx 10rpx 10rpx 0;
			}

			.right-box-warter {
				margin: 10rpx 0rpx 10rpx 10rpx;
			}
		}

	}
</style>